#include "Trip.h"

Trip::Trip(){}

wineryIn GetInfo()
{
	fstream inFile;
	string nWinery;
	int idWinery;
	int numWineries;
	float dCanyonVilla;
	int amtWines;
	string winesName;
	int winesYear;
	float winesPrice;
	wineryIn wineryInforma;
	int wineryNum;
	float dist;

	inFile.open("wineries.txt", ios::in);

	while (inFile)
	{
		getline (inFile, nWinery);
		inFile >> idWinery;
		inFile >>numWineries;

		distFromHere distancesbetween;
		for (int i=0; i < numWineries; i++)
		{
			inFile >> wineryNum;
			inFile>> dist;
			Distances newDistance(wineryNum, dist);
			distancesbetween.push_back(newDistance);
		}
		inFile >> dCanyonVilla;
		inFile >>amtWines;
		inFile.get();
		wines wineinformation;
		for (int j=0 ; j < amtWines; j++)
		{
			getline(inFile, winesName);
			inFile >> winesYear;
			inFile.ignore(5,'\n');
			inFile >>winesPrice;
			inFile.ignore(5,'\n');
			WineInfo wineNYP (winesName, winesYear, winesPrice);
			wineinformation.push_back (wineNYP);
		}
		Winery wineryInformation(nWinery, idWinery, distancesbetween,
				dCanyonVilla, amtWines, wineinformation);
		wineryInforma.push_back(wineryInformation);
	}
	cout<<endl;
	inFile.close();

	return wineryInforma;

}
void PrintInfo(wineryIn wineryInforma)
{
	wineryIn::iterator p;

	cout<<left<<setw(9)<<"Number" << setw(26)<<"Name" << "Distance"<<endl;
	for(p = wineryInforma.begin(); p < wineryInforma.end() - 1; ++p)
	{

		cout<<left<<setw(9)<<p->getWineryNumber();
		cout<<setw(26)<<p->getWineryName();
		cout<<right<<setw(8)<<fixed<<setprecision(2)<<p->getDistanceFromCV();
		cout<<endl;
	}
	cout<<endl;
}

void Trip::shortTrip(wineryIn wineryInforma, int idWinery[])
{
	int amtVisit;

	wineryIn::iterator p;
	distFromHere closeDist;
	distFromHere::iterator q;

	cout<<"How many wineries would you like to visit? ";
	cin >> amtVisit;

	cout<<endl;
	//sort the distances from the Hotel
	sort(wineryInforma.begin(), wineryInforma.end());

	p = wineryInforma.begin();

	int i=0;
	closeDist = p->getDistances();
	while (p<wineryInforma.end() && i<amtVisit)
	{

		//Sort those distances;
		sort (closeDist.begin(),closeDist.end());

		q=closeDist.begin();
		idWinery[i] = q->getWinery();
		closeDist.erase(closeDist.begin());
		p++;
		i++;
	}
	for(int j =0; j<amtVisit; j++)
	{
		p=find(wineryInforma.begin(), wineryInforma.end(),idWinery[j]);
		cout<<left<<setw(9)<<p->getWineryNumber();
		/////////////////////////////////////////////////////////////
		tourList.push_back(p->getWineryNumber());
		cout<<setw(26)<<p->getWineryName();
		cout<<right<<setw(8)<<fixed<<setprecision(2)<<p->getDistanceFromCV();
		cout<<endl;
	}
	cout<<endl;
}
void Trip::customTrip(wineryIn wineryInforma, int idWinery[])
{
	int amtVisit;
	wineryIn customWinery;		//vector to hold only the wineries
								//the user enter

	wineryIn::iterator p;
	distFromHere closeDist;
	distFromHere::iterator q;
	distFromHere distToUse;
	distFromHere::iterator r;
	vector <int> nums;
	vector <int>::iterator a;

    cout<<"How many wineries would you like to visit? ";
    cin >> amtVisit;

    cout<<"Please input the number ID of the winery you would like to visit"<<endl;
    for (int i =0; i<amtVisit; i++)
    {
    	cout<< "Winery #" << i+1 << ": ";
    	cin >> idWinery[i];
    }
    cout<<endl;

    //find those wineries
    for(int j =0; j<amtVisit; j++)
    {
    	p=find(wineryInforma.begin(), wineryInforma.end(),idWinery[j]);
    	customWinery.push_back(*p);
    }

    //sort close to find closest winery to the Canyon Villa
    sort (customWinery.begin(), customWinery.end());

    //p will contain the winery information vector
    p = customWinery.begin();
    int i=0;

    while (p<customWinery.end() && i <amtVisit)
    {
    	closeDist = p->getDistances();
       	int j =0;
    	distFromHere distancesbetween;

    	for (q=closeDist.begin();q<closeDist.end();q++)
    	{
    		if(idWinery[j]== q->getWinery())
    		{
    			Distances newDistance(q->getWinery(), q->getFromCurrent());
    			distancesbetween.push_back(newDistance);
    			j++;
    		}
    	}

    	distToUse = distancesbetween;
    	//sort the distancces within that winery
    	sort (distToUse.begin(),distToUse.end());

    	r=distToUse.begin();
    	//add id to vectors nums
    	nums.push_back(r->getWinery());
    	cout <<r->getWinery()<<"  "<<p->getWineryName()<<endl;
    	r++;

    	for (a=nums.begin(); a<nums.end(); ++a)
    	{
    		if (r->getWinery()== *a)
    		{
    			distToUse.erase (distToUse.begin());
    		}
    	}
		p=customWinery.begin();
    	while (p->getWineryNumber()!= r->getWinery() && p<customWinery.end() )
    	{
    		p++;
    	}
    	i++;
    }
   cout<<endl;
}

vector<int> Trip::getList(){return tourList;}
